package com.henry.server;
/*
 *  编程实现服务器的初始化和关闭
 */

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerInitClose {
    private ServerSocket ss;
    private Socket s;

    /*
     *  自定义成员方法实现服务器的初始化操作
     */

    public ServerInitClose() {
    }

    public ServerInitClose(int port) throws IOException {
        //创建ServerSocket类型的对象并提供端口号
        ss = new ServerSocket(port);
    }


    public void serverInit() throws IOException {
        while(true) {
            System.out.println("等待客户端的连接请求");
            s = ss.accept();
            ServerDao sd = new ServerDao();
            ServerView sv = new ServerView(sd);
            ServerThread st = new ServerThread(sv, s);
            new Thread(st).start();
            System.out.println(s.getInetAddress() + "连接成功！");
        }
    }

    /*
     *  自定义成员方法实现服务器的关闭操作
     */
    public void serverClose() throws IOException {
        ss.close();
    }
}
